<script setup>
import { onMounted, ref } from "vue"
import { Delete, Edit } from '@element-plus/icons-vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/util/format'
import { isLoading, toggleLoading } from '@/composables/loading'
// 条件参数
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
// 文章列表数据
const articleList = ref([])
const total = ref(0)
const getArticleList = async () => {
  toggleLoading()
  const res = await artGetListService(params.value)
  articleList.value = res.data
  total.value = res.total
  toggleLoading()
  // console.log(res)
}
// 获取数据
onMounted(async () => {
  await getArticleList()
})
//分页逻辑
const onSizeChange = async (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  await getArticleList()
}
const onCurrentChange = async (page) => {
  params.value.pagenum = page
  await getArticleList()
}
// 搜索逻辑
const onSearch = async () => {
  params.value.pagenum = 1
  await getArticleList()
}
//重置逻辑
const onReset = async () => {
  params.value.pagenum = 1
  params.value.pagesize = 5
  params.value.cate_id = ''
  params.value.state = ''
  await getArticleList()
}

// 编辑抽屉
const editDrawerRef = ref()
// 添加文章
const onAddArticle = () => {
  editDrawerRef.value.open({})
}
//编辑文章
const onEditArticle = (row) => {
  editDrawerRef.value.open(row)
}

// 添加修改成功
const onSuccess = async (type) => {
  if (type === 'add') {
    // 如果是添加，需要跳转渲染最后一页，编辑直接渲染当前页
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  await getArticleList()
}
// 删除功能
const onDeleteArticle = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getArticleList()
}
</script>

<template>
  <PageContainer title="文章管理">
    <!-- 定制头 -->
    <template #extra>
      <el-button type="primary" size="large" plain @click="onAddArticle()" ref="articleEditRef"
        @success="onSuccess">发表文章</el-button>
    </template>
    <!-- 定制内容 -->
    <!-- 筛选框 -->
    <el-form :inline="true" class="form-inline" :model="params">
      <el-form-item label="文章分类：">
        <channel-select v-model="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select placeholder="选择发布状态" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch" plain>搜索</el-button>
        <el-button @click="onReset" plain>重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 主题内容 -->
    <el-table :data="articleList" style="width: 100%" v-loading="isLoading" element-loading-text="加载中...">
      <el-table-column label="文章标题" width="400">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditArticle(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDeleteArticle(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination :current-page="params.pagenum" :page-size="params.pagesize" :page-sizes="[5, 10]"
      layout="jumper, total, sizes, prev, pager, next" background :total="total" @size-change="onSizeChange"
      @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end" />
  </PageContainer>

  <!-- 抽屉编辑文章 -->

  <ArticleEdit ref="editDrawerRef"></ArticleEdit>
</template>

<style lang="scss" scoped>
.form-inline {
  .el-select {
    --el-select-width: 220px;
  }

  .el-input {

    --el-input-width: 220px;
  }
}
</style>
